/**
 * @class UI-基础件-底部栏
 * @author DuskyHuang 圣子
 * @description 包含镜头速度、逃逸速度等UI显示
*/

import { _decorator, Label, Color, Component } from 'cc';
import { Mediator, utils } from 'cfw';
import { KM_OF_LY, SceneGraph } from 'const';
import { toThousands } from 'helpers';
import { UI } from 'notifications';
import { cosmos, ui } from 'proxies';
import { LabelI18n } from '../label-i18n';
const { ccclass, property } = _decorator;

@ccclass('cocosmos.foot-bar')
export class FootBar extends Mediator {

    @property(Label) lensVel: Label = null;
    @property(LabelI18n) escVel: LabelI18n = null;

    protected onLoad(): void {
        this._init();
        this._listen();
    }

    protected start(): void {
        this._updateStates();
    }

    protected update(dt: time.sec): void {
        const { pow10 } = cosmos;
        if (this.lensVel.node.activeInHierarchy) {
            this.lensVel.string = this._cameraAbsoluteVelocity;
        }
        if (this.escVel.node.activeInHierarchy) {
            const idx = CosmicVelocityValves.findIndex(value => pow10 < value);
            this.escVel.string = `bars@cv-${idx}`; // TODO 优化为针对每个center设置逃逸速度
            this.escVel.getComponent(Label).color = CosmicVelocityColors[idx];
        }
    }


    protected onLensPanelParams(): void {
        this._updateStates();
    }

    private _init(): void {
        this._rc = utils.findNode(MAIN, COSMOS).getComponent('cocosmos.relative-camera') as IRelativeCamera;
    }

    private _listen(): void {
        this.on(UI.LENS_PARAMS, this.onLensPanelParams);
    }

    private _updateStates(): void {
        this.lensVel.node.parent.active = ui.showLensVel;
        this.escVel.node.parent.active = ui.showEscVel;
    }

    private get _cameraAbsoluteVelocity(): string {
        const { pow10, delta } = cosmos, { angularVelocity } = this._rc;
        const tangent = Math.parseRadian(angularVelocity);  // 切向速度
        const radial = Math.abs(delta * 10);                // 径向速度
        const arc = Math.sqrt(Math.sumSqr(tangent, radial));// 球面微分
        const dist = Math.pow(10, pow10) * arc * KM_OF_LY;  // 光年转公里
        const speed = dist.toExponential();
        const [n, e] = speed.split('e');
        const nstr = n.replace('.', ''), len = nstr.length, ne = +e;
        let res: string;
        if (ne < 3) return '0km/s';
        if (len > ne) {
            res = nstr.substring(0, ne + 1);
        } else {
            const tail = (Math.random() + '').replace('.', '');
            const lack = (ne + 1) - len;
            res = nstr + tail.substring(0, lack);
        }
        return toThousands(res) + ' km/s';
    }

    private _rc: IRelativeCamera;
}

const { COSMOS, Cameras: { MAIN } } = SceneGraph;

const CosmicVelocityValves: pow10[] = [
    -99, -8, -6.5, -4, -1, 4, 7, 99
];
const CosmicVelocityColors: Color[] = [
    Color.GRAY,
    new Color(197, 197, 197, 255),
    new Color(132, 233, 186, 255),
    new Color(0, 255, 32, 255),
    new Color(255, 162, 0, 255),
    new Color(255, 102, 102, 255),
    new Color(255, 105, 236, 255),
    new Color(136, 161, 255, 255),
];
declare type IRelativeCamera = {
    angularVelocity: num.deg;
} & Component;